/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dopdf.io;

import org.dopdf.document.model.ResourceDetail;
import org.dopdf.utils.Constants;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * The class that will help us to scale the image instance to a smaller image which can be rednered on the browser.
 *
 * @author Deep Shah
 */
public class Image {

    /** The buffered image reference. */
    private BufferedImage bufferedImage;

    /**
     * The constructor that takes in buffered image as argument.
     * @param bufferedImage the buffered image that is to be scalled.
     */
    public Image(final BufferedImage bufferedImage) {
        this.bufferedImage = bufferedImage;
    }

    /**
     * Scales the image to the desired size.
     * @param size the size used to scale the image.
     * @return the BufferedImage reference pointing to the scalled image.
     */
    public ResourceDetail scale(final Constants.PageSize size) throws IOException {
        scaledImage(size);
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, Constants.Resource.JPEG_FORMAT, outputStream);
        outputStream.flush();
        return new ResourceDetail(outputStream.toByteArray(), Constants.Resource.JPEG_FORMAT);
    }

    /**
     * Scales the buffered image held to the given size.
     * @param size the size to which the image is to be scalled.
     * @return the buffered image reference that is scaled to the given size.
     */
    private void scaledImage(final Constants.PageSize size) {
        int type = (bufferedImage.getTransparency() == Transparency.OPAQUE) ?
            BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
        int w = bufferedImage.getWidth(), h = bufferedImage.getHeight();
        do {
            if (w > size.getWidth()) {
                w /= 2;
                if (w < size.getWidth()) {
                    w = size.getWidth();
                }
            }

            if (h > size.getHeight()) {
                h /= 2;
                if (h < size.getHeight()) {
                    h = size.getHeight();
                }
            }

            BufferedImage tmpImage = new BufferedImage(w, h, type);
            Graphics2D g2 = tmpImage.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2.drawImage(bufferedImage, 0, 0, w, h, null);
            g2.dispose();

            bufferedImage = tmpImage;
        } while (w != size.getWidth() || h != size.getHeight());
    }
}
